/*
 * @(#)ResourceDirective.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result.template.directive;

import java.io.IOException;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.*;

/**
 * Resource 파일(.res)에 포함된 uio를 include 하기 위한 {@link TemplateDirectiveModel}.
 *
 * @author Web Platform Development Team
 */
public class ResourceDirective extends AbstractTemplateDirectiveModel {

    /**
     * @param env      Environment
     * @param params   Map
     * @param loopVars TemplateModel
     * @param body     TemplateDirectiveBody
     * @throws TemplateException the TemplateException
     * @throws IOException       the IOException
     * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
     */
    @SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {

        Object paramId = params.get("id");

        if (paramId == null
                || (!(paramId instanceof TemplateScalarModel) && !(paramId instanceof TemplateSequenceModel))) {
            throw new TemplateException("Illegal Argumlent Error. id is null or invalidate type.", env);
        }

        Object resourceIds = null;

        if (paramId instanceof TemplateScalarModel) {
            resourceIds = env.getVariable(getAsString(paramId));
        }
        if (resourceIds == null) {
            resourceIds = paramId;
        }

        Configuration configuration = env.getConfiguration();
        String currentTemplateName = env.getTemplate().getName();

        if (resourceIds instanceof TemplateSequenceModel) {
            TemplateSequenceModel ids = (TemplateSequenceModel)resourceIds;
            int size = ((TemplateSequenceModel)resourceIds).size();

            for (int i = 0; i < size; i++) {
                TemplateScalarModel id = (TemplateScalarModel)ids.get(i);
                env.include(env.getConfiguration().getTemplate(
                        getIncludeTemplateName(currentTemplateName, id.getAsString())));
            }
        } else if (resourceIds instanceof TemplateScalarModel) {
            String id = ((TemplateScalarModel)resourceIds).getAsString();
            env.include(configuration.getTemplate(getIncludeTemplateName(currentTemplateName, id)));
            return;
        } else {
            throw new IllegalArgumentException("resource id [" + resourceIds + "] is illegal type");
        }
    }

    /**
     * Gets the include template name.
     *
     * @param currentTemplateName the current template name
     * @param uioName             the uio name
     * @return the include template name
     */
    private String getIncludeTemplateName(String currentTemplateName, String uioName) {
        int posFTL = currentTemplateName.indexOf(".ftl");
        String includeTemplateName = currentTemplateName.substring(0, posFTL) + ".[" + uioName + "]";
        log.debug("resourceName is " + includeTemplateName);
        return includeTemplateName;
    }
}
